package com.ljt.system.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljt.response.Result;
import com.ljt.system.entity.Role;
import com.ljt.system.service.RoleService;
import com.ljt.utils.ExcelUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author ljt
 * @since 2021-06-27
 */
@RestController
@RequestMapping("/system/role")
public class RoleController {
    @Autowired
    private RoleService roleService;


//    @ApiOperation(value = "用户列表", notes = "查询所有的用户信息")
//    @GetMapping("/queryAllRoleByPage")
//    public Result queryAllUserByPage(@RequestParam(required = false,defaultValue = "1") Integer current,
//                                     @RequestParam(required = false,defaultValue = "6") Integer size) {
//
//        Page<Role> rolePage = new Page<>(current, size);
//        rolePage = roleService.page(rolePage);
//        return Result.ok().data("rolePage", rolePage);
//    }


    @ApiOperation(value = "用户列表", notes = "查询所有的用户信息")
    @GetMapping("/queryAllRoleByPage")
    public Result queryUserByRoleName(@RequestParam(required = false,defaultValue = "1") Integer current,
                                     @RequestParam(required = false,defaultValue = "6") Integer size,
                                      @RequestParam String roleName, @RequestParam String remark) {

        Page<Role> rolePage = new Page<>(current, size);
        rolePage = roleService.queryUserByRoleName(rolePage,roleName,remark);
        return Result.ok().data("rolePage", rolePage);
    }

    @ApiOperation(value="角色信息",notes = "查询所有角色信息")
    @GetMapping("/queryAllRole")
    public Result queryAllRole(){
        List<Role> role= roleService.list();
        return Result.ok().data("role",role);
    }

    @ApiOperation(value="角色信息",notes="删除角色信息")
    @GetMapping("deleteRoleById")
    public Result deleteRoleById(Integer id){
        boolean i=roleService.removeById(id);
        if(i){
            return Result.ok();
        }else{
            return Result.error();
        }
    }

    @ApiOperation(value="角色信息",notes="修改角色信息")
    @PostMapping("/updateRoleById")
    public Result updateRoleById(@RequestBody Role role){
        Date date=new Date();
        role.setModifiedTime(date);
        boolean i=roleService.updateById(role);
        if(i){
            return Result.ok();
        }else{
            return Result.error();
        }
    }

    @ApiOperation(value="角色信息",notes="修改角色信息")
    @GetMapping("/updateRoleById2")
    public Result updateRoleById2(Role role){
        Date date=new Date();
        role.setModifiedTime(date);
        boolean i=roleService.updateById(role);
        if(i){
            return Result.ok();
        }else{
            return Result.error();
        }
    }

    @ApiOperation(value = "角色信息",notes="添加角色信息")
    @PostMapping("/insertRole")
    public Result insertRole(@RequestBody Role role){
        System.out.println(role);
        boolean i=roleService.save(role);
        if(i){
            return Result.ok();
        }else{
            return Result.error();
        }
    }


    @ApiOperation(value = "导出",produces="application/octet-stream")
    @GetMapping("/exportRoleToExcel")
    public void exportExcel(HttpServletResponse response) throws IOException {
        List<Role> roleList = roleService.list();
        ExcelUtil<Role> util = new ExcelUtil<Role>(Role.class);
        util.exportExcel(response,roleList , "角色数据");
    }

    @ApiOperation("导入")
    @PostMapping("/importRoleToExcel")
    public Result importExcel(@RequestBody MultipartFile file) throws Exception {
        ExcelUtil<Role> util = new ExcelUtil<>(Role.class);
        List<Role> roleList = util.importExcel(file.getInputStream());
        boolean insertFlag = roleService.saveBatch(roleList);
        if (insertFlag){
            return Result.ok();
        }else{
            return Result.error();
        }
    }
}

